import { useRedis } from '@sker/core';
import { useData } from '@sker/runtime';
import dayjs from 'dayjs';
import { z } from 'zod';

export default async () => {
    const input = z.object({
        name: z.optional(z.string()),
        link: z.optional(z.string()),
        online: z.optional(z.number()),
        openTime: z.string(),
        readPacketCount: z.optional(z.number()),
        rank: z.optional(z.string()),
        redPackets: z.array(z.any()),
        ksid: z.optional(z.string()),
        userId: z.optional(z.string()),
        shareObjectId: z.optional(z.string()),
        category: z.optional(z.string()),
    });
    const body = input.parse(useData());
    await useRedis(7, async r => {
        async function setWithExpiration(hashName: string, fieldName: string, fieldValue: string, expirationTime: number) {
            await r.hSet(hashName, fieldName, fieldValue);
            await r.expire(hashName, expirationTime);
        }
        const expireTime = Math.floor((dayjs(body.openTime).subtract(1, 'minute').toDate().getTime() - dayjs().toDate().getTime()) / 1000)
        await setWithExpiration('rooms', body.userId, JSON.stringify(body), expireTime)
        return;
    });
}